package systempen.cmanager;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DialogAddCost extends Dialog implements OnClickListener {
	Button okButton;
	Button cancelButton;
	EditText et;
	String level;
	IRefreshList irr;
	public DialogAddCost(Context context, String level, IRefreshList iref) {
		super(context);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_add_cost_type);
		okButton = (Button) findViewById(R.id.OkButton);
		okButton.setOnClickListener(this);
		cancelButton = (Button) findViewById(R.id.CancelButton);
		cancelButton.setOnClickListener(this);
		et = (EditText) findViewById(R.id.editTextCostName);
		this.level = level;
		irr = iref;
	}
	@Override
	public void onClick(View v) {
	/** When OK Button is clicked, dismiss the dialog */
	if (v == cancelButton)
	dismiss();
	if (v==okButton)
		if (et.getText().length()==0){			
			Toast.makeText(getContext(),"Cost type can't be empty.", Toast.LENGTH_LONG).show();
			dismiss();
		}
		else if (level.length() ==0){			
		Toast.makeText(getContext(),"Level can't be empty.", Toast.LENGTH_LONG).show();
		dismiss(); 
		}
		else{
			util.execSQL("insert into cost_type(name, parent) values ('"+et.getText().toString()+"',"+level+")", getContext());
			util.AzurirajPraznePathove(getContext());
			Toast.makeText(getContext(),"Cost type:"+et.getText().toString()+" successfully inserted!", Toast.LENGTH_LONG).show();
			onRefreshList();
			dismiss();
		}		
	}
    protected void onRefreshList(){
    	if (irr!=null)
    		irr.onRefreshList();
    }
    public interface IRefreshList{
    	public void onRefreshList();
    }
}
